package com.ybbase.framework.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ctc.wstx.util.DataUtil;
import com.ybbase.framework.common.enums.LogFunType;
import com.ybbase.framework.common.enums.LogType;
import com.ybbase.framework.common.util.DateUtil;
import com.ybbase.framework.model.po.system.Log;
import com.ybbase.framework.mapper.system.LogMapper;
import com.ybbase.framework.model.po.system.User;
import com.ybbase.framework.service.system.LogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author cg
 * @since 2019-11-25
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {
    @Autowired
    private LogMapper logMapper;

    /**
     * 保存日志到数据库
     *
     * @param logType
     * @param logFunType
     * @return
     */
    @Override
    public boolean saveLog(Integer logType, Integer logFunType) {
        String aa = LogType.getFromKey(logType);
        String bb = LogFunType.getFromKey(logFunType);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Log log = new Log();
        if (user!=null&&user.getId() != null) {
            String content = user.getName() + "在" + DateUtil.getTime(new Date()) + "操作了" + aa + bb + "功能";
            log.setCreateBy(user.getId());
            log.setCreateDate(new Date());
            log.setDelFlag(0);
            log.setType(logType);
            log.setContent(content);
            log.setTitle(aa + bb);
            logMapper.insert(log);
            return true;
        } else {
            return false;
        }
    }

}
